<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h = "http://java.sun.com/jsf/html"
      xmlns:f = "http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </h:head>
    <!-- Page to be shown when the user checks out
         Contains all items in the cart.
         The total sum is also shown.
         Pressing the buy now!-button finishes the purchase.
         The cart is then emptied and an e-mail is sent to the user
         The user needs to logged in to be able to purchase products.
         Otherwise a message will be displayed-->
    <h:body>
        <ui:composition template="/WEB-INF/facelets/template.xhtml">
            <ui:param name="title" value = "Check Out - Chalmers Food Store"/>
            <ui:define name="content">
                <!-- Updated by log in-button -->
                <h:form id="buyForm" >
                    <!-- To be displayed when the user is logged in -->
                    <!-- A list of items to be purchased -->
                    <h:panelGroup rendered="#{loginModelBean.loggedIn == true}">
                        <br/>
                        <h1>Items to purchase: </h1>
                        <hr/>
                        <h:form>
                            <p:dataTable value="#{shoppingCartModelBean.cart}" var="item">
                            <!-- First column begins here -->
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Name" />
                                </f:facet>
                                <h:outputText value="#{item.product.name}" />
                            </p:column>
                            <br/>
                            <!-- Second column begins here -->
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Category" />
                                </f:facet>
                                <h:outputText value="#{item.product.cat}" />
                            </p:column>
                            <br/>
                            <!-- Third column begins here -->
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Quantity" />
                                </f:facet>
                                <h:outputText value="#{item.quantity}"/>
                            </p:column>
                            <br/>
                            <!-- Fourth column begins here -->
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Unit Price" />
                                </f:facet>
                                <h:outputText value="#{item.product.price}" />
                            </p:column>
                            <!-- Fifth column begins here -->
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Total Price" />
                                </f:facet>
                                <h:outputText value="#{item.product.price * item.quantity}" />
                            </p:column>
                        </p:dataTable>
                        </h:form>
                        <!-- Show the total price -->
                        <h:outputText value="Total price of products: #{shoppingCartModelBean.totalPrice}" />
                        <br/>
                        <!-- There must be items in the cart to be able to purchase something!!! -->
                        <h:panelGroup rendered="#{shoppingCartModelBean.size > 0}" >
                            <br/>
                            <p:commandButton value="Buy Now!" action="confirm.xhtml?faces-redirect=true" actionListener="#{buyController.buyAndEmptyCart()}" />
                        </h:panelGroup>
                    </h:panelGroup>
                    <!-- Display a message if the user is not logged in -->
                    <h:panelGroup rendered="#{loginModelBean.loggedIn == false}">
                        <br/>
                        <h1>
                            <h:outputText value="You must be logged in to buy food!" />
                        </h1>
                        <h:link value="Register now" outcome="registerCustomer" />
                    </h:panelGroup>
                    <!-- Go back home -->
                    <br/>
                    <br/>
                    <h:link value="Return to start page" outcome="goToStart" />
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>
